;
var hepler = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this;
    this.name = "华夏游-驴友旅游保险"
    this.genProductInfo = function () {
        let days = [];
        let projects = [];
        for (i = 1; i < 31; i++) {
            days.push(i.toString() + "d")
        }

        plan = {
            planA: {
                "year": ["1-65"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["100000"],
                "accident_medical": ["10000"],
                "danger_sport": ["100000"],
                "airplane_death": ["100000"],
                "train_death": ["100000"],
                "ship_death": ["100000"],
                "bus_death": ["100000"],
                "heatstroke_death": ["100000"],
                "heatstroke_medical": ["10000"],
                "exigency_back": ["150000"],
                "relate_mourn": ["6000"],
                "accident_hospital": [""],
                "nervous_help": ["1"]
            },
            planB: {
                "year": ["1-65"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["300000"],
                "accident_medical": ["20000"],
                "danger_sport": ["300000"],
                "airplane_death": ["300000"],
                "train_death": ["300000"],
                "ship_death": ["300000"],
                "bus_death": ["300000"],
                "heatstroke_death": ["300000"],
                "heatstroke_medical": ["20000"],
                "exigency_back": ["150000"],
                "relate_mourn": ["10000"],
                "accident_hospital": ["50/天"],
                "nervous_help": ["1"]
            },
            planC: {
                "year": ["1-65"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["500000"],
                "accident_medical": ["30000"],
                "danger_sport": ["500000"],
                "airplane_death": ["500000"],
                "train_death": ["500000"],
                "ship_death": ["500000"],
                "bus_death": ["500000"],
                "heatstroke_death": ["500000"],
                "heatstroke_medical": ["30000"],
                "exigency_back": ["150000"],
                "relate_mourn": ["10000"],
                "accident_hospital": ["100/天"],
                "nervous_help": ["1"]
            }
        }
        for (k in plan) {
            data = hepler.product([
                plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["accident_medical"], plan[k]["danger_sport"], plan[k]["airplane_death"], plan[k]["train_death"], plan[k]["ship_death"], plan[k]["bus_death"], plan[k]["heatstroke_death"], plan[k]["heatstroke_medical"], plan[k]["exigency_back"], plan[k]["relate_mourn"], plan[k]["accident_hospital"], plan[k]["nervous_help"]
            ])
            for (i = 0; i < data.length; i++) {
                dt = hepler.formatProductInfo(data[i], plan[k]);
                projects.push(dt);
            }
        }
        return projects;

    }
    this.httpRequest = function (dt) {
        if (dt.accident_death == "100000") {
            planCd = "planid15246217167561"
        } else if (dt.accident_death == "300000") {
            planCd = "planid15246217167562"
        } else {
            planCd = "planid15246217167563"
        }
        params = {
            "startdate": "2020/06/05",
            "enddate": that.towardDate(dt.days),
            "items": planCd,
            "peoplecount": "1",
            "planextendvalue": "",
            "productcode": "EAK_L"
        }
        return new Promise(function (resolve, reject) {
            request({
                url: "http://www.epicc.com.cn/eproduct/conproposal/calculate.do",
                method: "POST",
                json: false,
                rejectUnauthorized: false,
                headers: {
                    "Accept": "application/json, text/plain, */*",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
                    "Cookie": "epicc_tid=epicc.1578282117557422; _gscu_321070471=7828211745c42110; s_fid=51BB77C19E20D92E-16021A351FFE693E; vid=ef0bea9d6c25528ee3c6714b85e0baef; epicc_ntid=AAAAAV6CpgMv3h/HwX4WAg==; svid=0C46E643B8EB2B5549E7AA5CA65A65FB; ag_fid=PqyKLr1Kk08UPYuF; ag_fid=PqyKLr1Kk08UPYuF; xY6qiPJDsR=MDAwM2IyNTRmNDgwMDAwMDAwMjUwKRMCa1UxNTkxMjMzNTM2; epiccnewEbsWeb=cUJ84zv90KQHv8F52zFel6LxSA5VCOGDzugR4DleNnTYchhvB21S!-896798546; R4XlxlrGUk=MDAwM2IyNTRmNDgwMDAwMDAwNWUwNn5mRBYxNTkxMjMzNTM2; epiccnewEbsWebeproduct=77586RXzxoHUX72V6faj3331l09HriAFGoVMAdXs6OkcnFuZD4e1!-1756725262; YXeuzFnfi6=MDAwM2IyNTRmNDgwMDAwMDAwNzYwbBV8PToxNTkxMjMzNTM2; personalCenter=RBl85NMFiqU8hJSn9glU0uTBm7gwJOostAmXDh4rGdCZ-RDeiQla!1679855540; _gscbrs_321070471=1; _gscs_321070471=91233549ttp1v313|pv:1; udmp_cm_sign_321070471=1; s_getNewRepeat=1591233549885-Repeat; s_vnum=1621564729064%26vn%3D29; s_cc=true; trkHmClickCoords=396%2C413%2C3903%2Chttp%3A%2F%2Fwww.epicc.com.cn%2Flvyoubaoxian%2Flvyoulvyoubaoxian%2F%23%2Fmain",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"
                },
                body: hepler.urlencode(params)
            }, function (err, res, body) {
                if (!err && res.statusCode == 200) {
                    if (body.indexOf("premium") != -1) {
                        price = JSON.parse(body)[3]["premium"].toFixed(2).toString();
                        rs = {
                            name: that.name,
                            company: "中国人民保险",
                            price: price
                        }
                        resolve(rs);
                    }
                }
            })
        })
    }
    this.towardDate = function (value = "3d") {

        let ontime = 86400000;
        let timestamp = Date.parse(new Date())
        let date = new Date(timestamp + (ontime * (Number(value.replace("d", "")))));
        year = date.getFullYear();
        month = (date.getMonth() + 1);
        day = date.getDate();
        if (month < 10) {
            if (day < 10) {
                _date = year + "/0" + month + "/0" + date.getDate();
            } else {
                _date = year + "/0" + month + "/" + date.getDate();
            }
        } else {
            if (day < 10) {
                _date = year + "/" + month + "/0" + date.getDate();
            } else {
                _date = year + "/" + month + "/" + date.getDate();
            }
        }

        return _date
    }
    this.main = async function () {
        let filter = [];
        data = that.genProductInfo();
        for (i = 0; i < data.length; i++) {
            dt = data[i]
            rs = await that.httpRequest(dt);
            price = rs.price;
            if (!filter.includes(price)) {
                filter.push(price);
                result = Object.assign(dt, rs);
                mysql.insertData(result)
            }
        }
        mysql.close()
    }
}
var spider = new spider();
spider.main();